package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Talentpool;
import org.example.models.R;
import org.example.service.TalentpoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/talentpool")
public class TalentpoolController {
    @Autowired
    private TalentpoolService talentpoolService;

    //查询状态人才，支持姓名或意向岗位模糊查询 （多表）
    @GetMapping("/queryStatus")
    public R<Page<Talentpool>> queryResumeRequirementsBasedOnStatus(
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "current", required = false, defaultValue = "1") int current,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size
    ) {
        Page<Talentpool> page = new Page<>(current, size); // 创建分页对象
        List<Talentpool> talentpools = talentpoolService.searchTalents(status, keyword); // 查询符合条件的数据
        int start = (current - 1) * size; // 计算起始索引
        int end = Math.min(start + size, talentpools.size()); // 计算结束索引
        List<Talentpool> subList = talentpools.subList(start, end); // 截取当前页数据
        page.setRecords(subList); // 设置当前页数据
        return R.success(page);
    }

    @GetMapping("/ascendingOrder")//升序
    public R<Page<Talentpool>> queryResumeRequirementsBasedOnStatusAscendingOrder(
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "current", required = false, defaultValue = "1") int current,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size
    ) {
        Page<Talentpool> page = new Page<>(current, size); // 创建分页对象
        List<Talentpool> talentpools = talentpoolService.searchTalentsAsc(status, keyword); // 查询符合条件数据
        int start = (current - 1) * size; // 计算起始索引
        int end = Math.min(start + size, talentpools.size()); // 计算结束索引
        List<Talentpool> subList = talentpools.subList(start, end); // 截取当前页数据
        page.setRecords(subList);
        return R.success(page);
    }

    @GetMapping("/descendingOrder")//降序
    public R<Page<Talentpool>> queryResumeRequirementsBasedOnStatusDescendingOrder(
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "current", required = false, defaultValue = "1") int current,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size
    ) {
        Page<Talentpool> page = new Page<>(current, size);
        List<Talentpool> talentpools = talentpoolService.searchTalentsDesc(status, keyword);
        int start = (current - 1) * size;
        int end = Math.min(start + size, talentpools.size());
        List<Talentpool> subList = talentpools.subList(start, end);
        page.setRecords(subList);
        return R.success(page);
    }

    // 通过人才简历ID查询人才
    @GetMapping("/findTalentWithResumeByResumeId")
    public R<List<Talentpool>> findTalentWithResumeByResumeId(
            @RequestParam(value = "talentResumeId", required = false) String talentResumeId
    ) {
        List<Talentpool> talentWithResumeByResumeId = talentpoolService.findTalentWithResumeByResumeId(talentResumeId);
        return R.success(talentWithResumeByResumeId);
    }

    @PostMapping("/insert")
    public R insert(@RequestBody Talentpool talentpool,
                    @RequestAttribute("userId") String userId) {
        talentpool.setWarehousingDate(new Date());
        talentpool.setTalentstatus("待面试");
        talentpool.setTalentremarks("人才");
        talentpool.setCreator(userId);
        boolean save = talentpoolService.save(talentpool);
        if (save) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }
}
